package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
public class FMPA extends Model{
	
	@Required
	@ManyToOne
	public CIS cis;
	
	@Required
	public Date date;
	
	@Required
	@ManyToOne
	public Thematique thematique;
	
	public String sousThematique;
	
	@Column(columnDefinition = "TEXT")
	public String description;
	
	@ManyToOne
	@Required
	public Duree duree;
	
	public String observation;
	
	@ManyToOne
	public EtatFMPA etat;
	
	@OneToMany(cascade=CascadeType.ALL)
	public List<Participation> participants;

	public FMPA(CIS cis, Date date, Thematique thematique, String sousThematique, String description,
			Duree duree, String observation, EtatFMPA etat) {
		this.cis = cis;
		this.date = date;
		this.thematique = thematique;
		this.sousThematique = sousThematique;
		this.description = description;
		this.duree = duree;
		this.observation = observation;
		this.etat = etat;
		this.participants =new ArrayList<Participation>();
	}
	
	public void addParticipant(Participation p){
		participants.add(p);
	}
	
	//a vérifier
	public String toString(){
		return this.description;
	}
	
	public boolean equals(FMPA f){
		return f.cis.equals(this.cis)&&f.thematique.equals(this.thematique)&&f.date.equals(this.date);
	}
	
	public int getEffectif(){
		int effectif = 0;
		Iterator<Participation> it=this.participants.iterator();
		while(it.hasNext()){
			Participation p=it.next();
			if(p.role.id_rolefmpa.equals("FORME")){
				effectif++;
			}
		}
		  return effectif;
	}
}
